//
//  SPAfterSaleDetailViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/22.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPAfterSaleDetailViewController.h"
#import "SPCommon.h"
#import "SPOrderDetailHeaderView.h"
#import "SPOrderListTableViewCell.h"
#import "SPOrderInfoView.h"
#import "SPReasonDescView.h"
#import "SPOrderDetailSectionHeaderView.h"
#import "SPOrderProgressTableViewController.h"
#import "SPWriteOrderInfoViewController.h"
#import "SPOrderDetailModel.h"


@interface SPAfterSaleDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *writeBtn;
@property (weak, nonatomic) IBOutlet UIButton *refuseBtn;
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
@property (weak, nonatomic) IBOutlet UIView *progressV;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *progressVH;
@property (assign, nonatomic) NSInteger order_id;
@property (nonatomic,strong) SPOrderDetailModel *orderDetail;


@end

@implementation SPAfterSaleDetailViewController

- (instancetype)initWithOrder_id:(NSInteger)order_id
{
    if(self =[super init]){
        self.order_id = order_id;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"售后详情";

    [self setupTableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.writeBtn.layer.cornerRadius = 4;
    self.refuseBtn.layer.cornerRadius = 4;
    self.agreeBtn.layer.cornerRadius = 4;
    self.writeBtn.hidden = YES;
    
    self.progressVH.constant = 0;
    self.progressV.hidden = YES;
    [self fetchOrderDetail];
}



- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.tableView.tableHeaderView.height = 85;
}


- (void)setupTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    SPOrderDetailHeaderView *headerV = [SPOrderDetailHeaderView shareHeaderView];
    self.tableView.tableHeaderView = headerV;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPOrderListTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([SPOrderListTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPOrderInfoView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass([SPOrderInfoView class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPOrderDetailSectionHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass([SPOrderDetailSectionHeaderView class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPReasonDescView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass([SPReasonDescView class])];
}


#pragma mark--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section == 0?self.orderDetail.goods.count:0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
{
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return section == 0?180:UITableViewAutomaticDimension;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = nil;
    if(section ==0 ){
        SPOrderInfoView *infoV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([SPOrderInfoView class])];
        infoV.line_15.hidden = NO;
        infoV.model = self.orderDetail.order;
        view = infoV;
        
    }else{
        SPReasonDescView *desV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([SPReasonDescView class])];
        view = desV;
    }
    
    return view;
}



- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SPOrderDetailSectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([SPOrderDetailSectionHeaderView class])];
    headerV.title.text = [NSString stringWithFormat:@"商品(%ld)",self.orderDetail.goods.count];
    headerV.des.text = @"换货";
    return headerV;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPOrderListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SPOrderListTableViewCell class])];
    cell.model = self.orderDetail.goods[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}



#pragma mark--订单详情
- (void)fetchOrderDetail
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=order_detail&token=568e86c8630f7b44f29a23ba211d9f25
     参数 order_id 订单id
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"order_detail";
    dict[@"order_id"] = @(self.order_id);
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            self.orderDetail = [SPOrderDetailModel mj_objectWithKeyValues:res[@"data"]];
            [self.tableView reloadData];
        }else{
            [self.tableView reloadData];
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        [self.tableView reloadData];
    }];
}


#pragma mark--填写运单
- (IBAction)writeBtnOnClick:(UIButton *)sender {
    SPWriteOrderInfoViewController *info = [[SPWriteOrderInfoViewController alloc] initWithOrder_id:_order_id type:OrderType_SH];
    [self.navigationController pushViewController:info animated:YES];
}


#pragma mark--拒绝
- (IBAction)refuseBtnOnClick:(UIButton *)sender {
    [self operationWithStatus:2 complete:^{
        RZLog(@"----拒绝");
    }];
}


#pragma mark--同意
- (IBAction)agreeBtnOnClick:(UIButton *)sender {
    
    [self operationWithStatus:1 complete:^{
        self.progressV.hidden = NO;
        self.writeBtn.hidden = NO;
        self.progressVH.constant = 85;
        [self.tableView layoutIfNeeded];
        CGRect rect = self.tableView.frame;
        rect.size.height -=85;
        rect.origin.y += 85;
        self.tableView.frame = rect;
        RZLog(@"----同意");
    }];
}


#pragma mark--同意||拒绝
- (void)operationWithStatus:(NSInteger)status complete:(void(^)(void))completeBlock
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=shouhou_status&token=568e86c8630f7b44f29a23ba211d9f25
     参数 status 【1 同意 2 拒绝】 shouhou_id 售后id
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"shouhou_status";
    dict[@"shouhou_id"] = @(self.order_id);
    dict[@"status"] = @(status);
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            !completeBlock?:completeBlock();
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}



#pragma mark--进度条
- (IBAction)onClickProgress:(UIButton *)sender {
    [self.navigationController pushViewController:[SPOrderProgressTableViewController new] animated:YES];
}

@end
